/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Threading;

namespace Microsoft.Scripting {
    [AttributeUsage(AttributeTargets.All)]
    internal sealed class ResourceUtilsDescriptionAttribute : DescriptionAttribute {

        private bool replaced = false;

        /// <summary>
        ///     Constructs a new sys description.
        /// </summary>
        /// <param name='description'>
        ///     description text.
        /// </param>
        public ResourceUtilsDescriptionAttribute(string description)
            : base(description) {
        }

        /// <summary>
        ///     Retrieves the description text.
        /// </summary>
        /// <returns>
        ///     description
        /// </returns>
        public override string Description {
            get {
                if (!replaced) {
                    replaced = true;
                    DescriptionValue = ResourceUtils.GetString(base.Description);
                }
                return base.Description;
            }
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    internal sealed class ResourceUtilsCategoryAttribute : CategoryAttribute {

        public ResourceUtilsCategoryAttribute(string category)
            : base(category) {
        }

        protected override string GetLocalizedString(string value) {
            return ResourceUtils.GetString(value);
        }
    }

    /// <summary>
    ///    AutoGenerated resource class. Usage:
    ///
    ///        string s = ResourceUtils.GetString(ResourceUtils.MyIdenfitier);
    /// </summary>

    internal sealed class ResourceUtils {

        #region Generated Resource constants

        // *** BEGIN GENERATED CODE ***
        // generated by function: gen_res_contants from: generate_resource_constants.py

        internal const string CantAddCasing = "CantAddCasing";
        internal const string CantAddIdentifier = "CantAddIdentifier";
        internal const string CantDelete = "CantDelete";
        internal const string CantReadProperty = "CantReadProperty";
        internal const string CantWriteProperty = "CantWriteProperty";
        internal const string EmptySymbolId = "EmptySymbolId";
        internal const string EmptySymbolId1 = "EmptySymbolId1";
        internal const string IllegalNew_GenericParams = "IllegalNew_GenericParams";
        internal const string InvalidCtorImplementation = "InvalidCtorImplementation";
        internal const string InvalidOperation_ContainsGenericParameters = "InvalidOperation_ContainsGenericParameters";
        internal const string InvalidOperation_DefineParameterBakedMethod = "InvalidOperation_DefineParameterBakedMethod";
        internal const string InvalidOperation_SetThis = "InvalidOperation_SetThis";
        internal const string InvalidOperation_ThisInStaticMethod = "InvalidOperation_ThisInStaticMethod";
        internal const string InvalidOperation_TooManyArguments = "InvalidOperation_TooManyArguments";
        internal const string InvalidOperation_UnexpectedType = "InvalidOperation_UnexpectedType";
        internal const string InvalidOptionValue = "InvalidOptionValue";
        internal const string MissingOptionValue = "MissingOptionValue";
        internal const string MissingService = "MissingService";
        internal const string MissingType = "MissingType";
        internal const string NameNotDefined = "NameNotDefined";
        internal const string NotImplemented = "NotImplemented";
        internal const string NotImplemented_Assign = "NotImplemented_Assign";
        internal const string NotImplemented_EnumEmit = "NotImplemented_EnumEmit";
        internal const string StaticAccessFromInstanceError = "StaticAccessFromInstanceError";
        internal const string StaticAssignmentFromInstanceError = "StaticAssignmentFromInstanceError";
        internal const string TypeMustBeVisible = "TypeMustBeVisible";
        internal const string UnhandledException = "UnhandledException";
        internal const string Unit_NotDebuggerVisible = "Unit_NotDebuggerVisible";
        internal const string UnknownLanguageId = "UnknownLanguageId";
        internal const string UnknownLanguageProviderType = "UnknownLanguageProviderType";
        internal const string VerificationException = "VerificationException";

        // *** END GENERATED CODE ***

        #endregion



        static ResourceUtils loader = null;
        ResourceManager resources;

        internal ResourceUtils() {
            resources = new System.Resources.ResourceManager("Microsoft.Scripting.Core", this.GetType().Assembly);
        }

        private static ResourceUtils GetLoader() {
            if (loader == null) {
                ResourceUtils sr = new ResourceUtils();
                Interlocked.CompareExchange(ref loader, sr, null);
            }
            return loader;
        }

        private static CultureInfo Culture {
            get { return null/*use ResourceManager default, CultureInfo.CurrentUICulture*/; }
        }

        public static ResourceManager Resources {
            get {
                return GetLoader().resources;
            }
        }

        public static string GetString(string name, params object[] args) {
            ResourceUtils sys = GetLoader();
            if (sys == null)
                return null;
            string res = sys.resources.GetString(name, ResourceUtils.Culture);

            if (args != null && args.Length > 0) {
                for (int i = 0; i < args.Length; i++) {
                    String value = args[i] as String;
                    if (value != null && value.Length > 1024) {
                        args[i] = value.Substring(0, 1024 - 3) + "...";
                    }
                }
                return String.Format(CultureInfo.CurrentCulture, res, args);
            } else {
                return res;
            }
        }

        public static string GetString(string name) {
            ResourceUtils sys = GetLoader();
            if (sys == null)
                return null;
            return sys.resources.GetString(name, ResourceUtils.Culture);
        }

        public static object GetObject(string name) {
            ResourceUtils sys = GetLoader();
            if (sys == null)
                return null;
            return sys.resources.GetObject(name, ResourceUtils.Culture);
        }
    }
}
